package com.taiyuan.zfmvp.JUC.lock;

import java.util.concurrent.TimeUnit;

/**
 * @param
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption:
 * @Date 2022/10/5
 * @return
 */
public class DeadLock {

    //创建两个对象
    static Object a = new Object();
    static Object b = new Object();

    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {

                synchronized (a){
                    System.out.println(Thread.currentThread().getName()+"持有锁a，试图获取锁b");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (b){
                        System.out.println(Thread.currentThread().getName()+"获取锁b");
                    }
                }

            }
        },"a").start();




        new Thread(new Runnable() {
            @Override
            public void run() {

                synchronized (b){
                    System.out.println(Thread.currentThread().getName()+"持有锁b，试图获取锁a");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (a){
                        System.out.println(Thread.currentThread().getName()+"获取锁a");
                    }
                }
            }
        },"b").start();

    }

}
